package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmy.cultivate.entity.Quarter;
import com.xmy.cultivate.entity.views.ResLossData;
import com.xmy.cultivate.service.IQuarterService;
import com.xmy.cultivate.service.IStudentService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/api/workbench")
public class WorkbenchController {

    @Autowired
    IStudentService iStudentService;

    @Autowired
    IQuarterService iQuarterService;

    @Autowired //如果给redis中保存数据会使用默认的序列化机制，导致redis中保存的对象不可视
    RedisTemplate<Object, Object> redisTemplate;

    /**
     * 获取流失数据
     * @return
     */
    @RequiresPermissions(value = "dashboard:index")
    @GetMapping("/getLossData")
    public Result getLossData(@RequestParam(value = "subjectsId",required = false) Long subjectsId,
                              @RequestParam(value = "yearPart",required = false) String yearPart,
                              @RequestParam(value = "quarterNum",required = false) String quarterNum,
                              @RequestParam(value = "lessonType",required = false,defaultValue = "1") Integer lessonType){

        String redisIdKey = "lossData::"+yearPart+"-"+quarterNum+"-"+lessonType+"-"+subjectsId;
        Object o = redisTemplate.opsForValue().get(redisIdKey);
        ResLossData resLossData = new ResLossData();
        //redisTemplate.opsForValue().set(redisIdKey,null);
        if(Objects.isNull(o)){
            Quarter quarterNow = iQuarterService.getNowQuarter();
            Long quarterId = quarterNow.getId();
            if (StringUtils.isNotBlank(yearPart) && StringUtils.isNotBlank(quarterNum)){
                Quarter quarterParam = iQuarterService.getQuarterForYearAndNum(Integer.parseInt(yearPart),Integer.parseInt(quarterNum));
                if (quarterParam!=null){
                    quarterId = quarterParam.getId();
                }else {
                    quarterId = 0L;//不存在，设置为0
                }
            }
            Long startQuarterId = quarterId - 2;
            Long endQuarterId  = quarterId;
            resLossData = iStudentService.getLossData(subjectsId,lessonType,startQuarterId,endQuarterId);
            redisTemplate.opsForValue().set(redisIdKey,resLossData,3, TimeUnit.HOURS);
        }else {
            resLossData = (ResLossData)o;
        }
        return new Result(Code.OK,resLossData,"获取成功");

    }
}
